import os
import shutil

# 源文件夹路径
source_folder = "/Volumes/jdssd5/H图片/Pixiv/"

# 遍历源文件夹下的所有文件
for root, dirs, files in os.walk(source_folder):
    for file in files:
        file_path = os.path.join(root, file)
        # 找到文件名中第一个数字或特殊字符的位置
        index = 0
        for i, c in enumerate(file):
            if c.isdigit() or c in ["_", "-", " ", ".", "(", ")", "[", "]"]:
                index = i
                break
        # 获取文件夹名称
        folder_name = file[:index].strip()
        # 创建目标文件夹路径
        target_folder = os.path.join(source_folder, folder_name)
        if not os.path.exists(target_folder):
            os.makedirs(target_folder)
        # 移动文件到目标文件夹
        shutil.move(file_path, os.path.join(target_folder, file))